package demo;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Frame2 extends JFrame {
	JPanel contentPane;
	DefaultMutableTreeNode gen = new DefaultMutableTreeNode();
	JTree jTree1 = new JTree(gen);
	JButton jButton1 = new JButton();

	public Frame2() {
		gen.setUserObject("根");
		try {
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			jbInit();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	/**
	 * Component initialization.
	 *
	 * @throws Exception
	 */
	private void jbInit() throws Exception {
		contentPane = (JPanel) getContentPane();
		contentPane.setLayout(null);
		setSize(new Dimension(400, 300));
		setTitle("Frame Title");
		jTree1.setBounds(new Rectangle(84, 40, 222, 167));
		jButton1.setBounds(new Rectangle(137, 238, 129, 44));
		jButton1.setText("jButton1");
		jButton1.addActionListener(new Frame2_jButton1_actionAdapter(this));
		contentPane.add(jTree1);
		contentPane.add(jButton1);

		DefaultMutableTreeNode zhi1 = new DefaultMutableTreeNode("枝1");
		DefaultMutableTreeNode zhi2 = new DefaultMutableTreeNode("枝2");
		DefaultMutableTreeNode zhi3 = new DefaultMutableTreeNode("枝3");

		DefaultMutableTreeNode ye1 = new DefaultMutableTreeNode("叶1");
		DefaultMutableTreeNode ye2 = new DefaultMutableTreeNode("叶2");

		DefaultMutableTreeNode ye3 = new DefaultMutableTreeNode("叶3");

		zhi1.add(ye1);
		zhi1.add(ye2);
		zhi1.add(ye3);

		DefaultMutableTreeNode ye4 = new DefaultMutableTreeNode("叶4");
		DefaultMutableTreeNode ye5 = new DefaultMutableTreeNode("叶5");
		DefaultMutableTreeNode ye6 = new DefaultMutableTreeNode("叶6");
		DefaultMutableTreeNode ye7 = new DefaultMutableTreeNode("叶7");

		zhi2.add(ye4);
		zhi2.add(ye5);
		zhi2.add(ye6);
		zhi2.add(ye7);

		gen.add(zhi1);
		gen.add(zhi2);
		gen.add(zhi3);
	}

	public void jButton1_actionPerformed(ActionEvent e) {
		DefaultMutableTreeNode gen = (DefaultMutableTreeNode) jTree1.getModel()
				.getRoot();
		ArrayList list = new ArrayList();

		for (int i = 0; i < gen.getChildCount(); i++) {
			String[] str = new String[gen.getChildAt(i).getChildCount()];

			for (int a = 0; a < gen.getChildAt(i).getChildCount(); a++) {
				str[a] = gen.getChildAt(i).getChildAt(a).toString();
			}

			list.add(str);
		}

		for (int i = 0; i < list.size(); i++) {
			String[] str = (String[]) list.get(i);
			System.out.println("第" + (i + 1) + "层");
			for (String a : str) {
				System.out.print(a + "\t");
			}
			System.out.println("");
		}
	}

	public static void main(String[] args) {
		new Frame2().setVisible(true);
	}

}

class Frame2_jButton1_actionAdapter implements ActionListener {
	private Frame2 adaptee;

	Frame2_jButton1_actionAdapter(Frame2 adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {
		adaptee.jButton1_actionPerformed(e);
	}
}
